package com.hong.strategy.register;

import com.hong.Config.ConfigManager;
import com.hong.RpcServer;
import com.hong.config.constant.ServerConfig;
import com.hong.listener.Listener;
import com.hong.listener.RpcServerListener;
import com.hong.listener.impl.Registrar;
import com.hong.listener.impl.RpcServerConfigurer;
import com.hong.qrpc.codec.impl.JSONDecoder;
import com.hong.qrpc.codec.impl.JSONEncoder;
import com.hong.qrpc.utils.StringUtils;
import com.hong.strategy.PropertiesStrategy;

import java.util.HashSet;
import java.util.Map;

/**
 * @Author HQT
 * @Date 2022/7/23 10:48
 */
public class ServiceExportStrategy implements PropertiesStrategy {
    @Override
    public void disposeProperties(RpcServer rpcServer) {
        ConfigManager configManager = rpcServer.getConfigManager();
        Map<Object, Object> profile = configManager.getProfile();
        String needExport= (String) profile.get(ServerConfig.QRPC_SERVICE_EXPORT);
        if(!StringUtils.isEmpty(needExport)){
            Map<Class<? extends Listener>, HashSet<Listener>> listenerSets = rpcServer.getListenerManager().getListenerSets();
            HashSet<Listener> listeners = listenerSets.get(RpcServerListener.class);
            if(listeners==null){
                listeners=new HashSet<>();
                listenerSets.put(RpcServerListener.class,listeners);
            }
            Registrar registrar = new Registrar();
            if(!listeners.contains(registrar)){
                listeners.add(registrar);
            }
            registrar.setNeedExport(Boolean.parseBoolean(needExport));
        }
    }
}
